<?php
/**
 * S3 Rest Request 
 * @package dd_aws
 */

require_once('dd_aws_RestRequest.php');

/**
 * S3 Rest Request
 * @package dd_aws
 */
class dd_aws_s3_RestRequest extends dd_aws_RestRequest {

    /**
     * Client
     */
    private $client;

    /**
     * Input file
     */
    private $inFile = null;

    /**
     * Input file size
     */
    private $inFileSize = null;

    /**
     * Input file content type
     */
    private $inFileContentType = null;

    /**
     * Input file md5
     */
    private $inFileMd5 = null;

    /**
     * Automatically close inFile?
     */
    private $inFileAutoClose = false;

    /**
     * Constructor
     * @param dd_aws_s3_Client $client Client
     * @param string $accessKey Access Key ID
     * @param string $secretAccessKey Secret Access Key
     * @param string $verb Verb
     * @param string $hostname Hostname
     * @param string $resource Resource
     * @param array $headers Associative array of headers
     * @param array $amzHeaders Associative array of Amazon headers
     * @param array $curlOpts Associative array of curl options
     * @param bool $useSsl Use SSL?
     */
    public function __construct($client, $accessKey, $secretAccessKey, $verb, $hostname, $resource, $headers = null, $amzHeaders = null, $curlOpts = null, $useSsl = false) {
        parent::__construct($accessKey, $secretAccessKey, $verb, $hostname, $resource, $headers, $amzHeaders, $curlOpts, $useSsl);
        $this->client = $client;
    }

    /**
     * Set input file
     */
    public function setInfile($inFile) {
        if ( is_array($inFile) ) {
            list($inFile, $inFileSize, $inFileContentType, $inFileMd5) = $inFile;
            $this->inFile = $inFile;
            $this->inFileContentType = mime_content_type($inFile);
            $this->inFileSize = $inFileSize;
            $this->inFileMd5 = $inFileMd5;
        } else {
            $this->inFile = fopen($inFile, 'r');
            $this->inFileContentType = mime_content_type($inFile);
            $this->inFileSize = filesize($inFile);
            $this->inFileMd5 = base64_encode(md5_file($inFile, true));
            $this->inFileAutoClose = true;
        }
    }

    /**
     * s3 specific REST request preparation
     */
    protected function prepare() {

        parent::prepare();

        // All s3 requests require a date header.
        $this->setHeader('Date', gmdate('D, d M Y H:i:s T'));

        if ( $this->inFile !== null ) {
            $this->setCurlOpt(CURLOPT_INFILE, $this->inFile);
            $this->setCurlOpt(CURLOPT_INFILESIZE, $this->inFileSize);
            $this->setHeader('Content-MD5', $this->inFileMd5);
            $this->setHeader('Content-Length', $this->inFileSize);
            $this->setHeader('Content-Type', $this->inFileContentType);
        }

    }

    /**
     * s3 specific REST request authentication
     */
    protected function generateAuthenticationHeader() {

        $amzHeaders = array();
        $amzHeadersString = '';
        foreach ( $this->amzHeaders() as $header => $value ) {
            $amzHeaders[] = strtolower($header) . ':' . $value;
        }

        if ( sizeof($amzHeaders) > 0 ) {
            sort($amzHeaders);
            $amzHeadersString = implode("\n", $amzHeaders) . "\n";
        }

        return 'AWS ' . $this->accessKey() . ':' . $this->generateAwsHash(
            $this->verb() . "\n" .
            $this->getHeader('Content-MD5') . "\n" .
            $this->getHeader('Content-Type') . "\n" .
            $this->getHeader('Date') . "\n" .
            $amzHeadersString .
            preg_replace('/\?.*$/', '', $this->resource())
        );

    }

}

?>
